//
//  ToutiaoArticleViewController.m
//  BobNews
//
//  Created by lanou3g on 16/5/5.
//  Copyright © 2016年 liu. All rights reserved.
//

#import "ToutiaoArticleViewController.h"
#import "NetWorkRequestManager.h"
#import "DBManager.h"
#import "UserInfoManager.h"
#import "LoginViewController.h"
#import "TTCommentTableController.h"
// 分享
#import "UMSocial.h"
//#import "NSString+Html.h"
@interface ToutiaoArticleViewController ()<UIWebViewDelegate,UMSocialUIDelegate>

@property (nonatomic,strong) UIImage *img;
@property (nonatomic,strong) UIImage *img2;
@property (nonatomic,strong) NSString *imgName;
@property (nonatomic,strong) NSString *html;
@end

@implementation ToutiaoArticleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.title = self.navTitle;
    
    [self analyticalData];
    
    /// 收藏
    self.imgName = nil;
    self.img = [UIImage imageNamed:self.imgName];
    if ([[DBManager shareManager]hasCollectionDocid:self.docid]) {
        self.imgName = @"icon_collect_highlight.png";
    } else {
        self.imgName = @"icon_collect.png";
    }
    self.img = [UIImage imageNamed:self.imgName];
    self.img2 = [self.img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *collectionBtn = [[UIBarButtonItem alloc]initWithImage:self.img2 style:UIBarButtonItemStylePlain target:self action:@selector(didCollectionBtn:)];
    
    /// 分享
    UIImage *image1 = [UIImage imageNamed:@"分享.png"];
    UIImage *image2 = [image1 imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(shareBtn:)];
    
    
    
    UIBarButtonItem *commentBtn = [[UIBarButtonItem alloc]initWithTitle:@"评论" style:UIBarButtonItemStylePlain target:self action:@selector(didClickCommentBtn:)];
    
    self.navigationItem.rightBarButtonItems = @[collectionBtn,shareBtn,commentBtn];
}
/// 评论
- (void)didClickCommentBtn:(UIBarButtonItem *)sender {
    TTCommentTableController *commentTVC = [[TTCommentTableController alloc]init];
    commentTVC.d = self.docid;
    
    [self.navigationController pushViewController:commentTVC animated:YES];
}
#pragma mark - 收藏
- (void)didCollectionBtn:(UIBarButtonItem *)sender {
    if ([UserInfoManager shareManager].uid) {
        if ([self.imgName isEqualToString:@"icon_collect.png"]) {
            [[DBManager shareManager]collectionModel:self.model html:self.html];
            self.imgName = @"icon_collect_highlight.png";
            self.img = [UIImage imageNamed:self.imgName];
            self.img2 = [self.img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [sender setImage:self.img2];
        } else {
            [[DBManager shareManager]cancelCollectionArtical:self.model.docid];
            self.imgName = @"icon_collect.png";
            self.img = [UIImage imageNamed:self.imgName];
            self.img2 = [self.img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [sender setImage:self.img2];
        }
    } else {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录" preferredStyle:UIAlertControllerStyleAlert];
        [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self login];
        }]];
        [alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController:alertC animated:YES completion:nil];
    }
}
/// 分享
- (void)shareBtn:(UIBarButtonItem *)sender {
    if ([UserInfoManager shareManager].uid) {
        [UMSocialSnsService presentSnsIconSheetView:self appKey:@"564913af67e58ed0d7005bb3" shareText:self.html shareImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.model.imgsrc]]] shareToSnsNames:[NSArray arrayWithObjects:UMShareToDouban,UMShareToTencent,UMShareToRenren,UMShareToSina,UMShareToQQ,UMShareToWechatSession, nil] delegate:self];
    } else {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录" preferredStyle:UIAlertControllerStyleAlert];
        [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self login];
        }]];
        [alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }]];
        [self presentViewController:alertC animated:YES completion:nil];
    }
}

#pragma mark - 跳转到登录
- (void)login {
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    [self.navigationController pushViewController:loginVC animated:YES];
}

#pragma mark - 网络数据
///http://c.m.163.com/nc/article/BM9NGT2N00031GVS/full.html
- (void)analyticalData {
    
    UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:webView];
    
    NSString *str = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html",self.docid];
    [NetWorkRequestManager requestUrl:[NSURL URLWithString:str] RequestMethod:GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDic = jsonDic[self.docid];
            self.html = dataDic[@"body"];
            NSLog(@"%@",self.html);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [webView loadHTMLString:self.html baseURL:nil];
            });
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
